package com.sm.scorpio.ui.activity.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.scorpio.baselibrary.utils.AppDataUtil;
import com.sm.imlibrary.imsdk.SIMManager;
import com.sm.imlibrary.nio.LoginCallbackListener;
import com.sm.scorpio.R;
import com.sm.scorpio.http.HttpNormalCallback;
import com.sm.scorpio.http.HttpUtils;
import com.sm.scorpio.ui.activity.MainActivity;
import com.sm.scorpio.ui.base.BaseActivity;
import com.sm.scorpio.ui.dialog.ContentDialog;
import com.sm.scorpio.ui.dialog.DialogManager;
import com.sm.scorpio.ui.bean.model.UserBean;
import com.sm.scorpio.ui.bean.http.UserHttpBean;

public class LoginActivity extends BaseActivity {
    @Override
    protected int onCreateLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        setBasicTitle(R.string.string_00009);
        v.addOnClickListener(this, R.id.login_btn);
        v.getEditText(R.id.login_username_edit).addTextChangedListener(textWatcher);
        v.getEditText(R.id.login_password_edit).addTextChangedListener(textWatcher);
        v.setText(R.id.login_username_edit, "admin");
        v.setText(R.id.login_password_edit, "123456");
        updateLoginBtnState();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.login_btn:
                skipActivityFinish(MainActivity.class);
                //loginHttp();
                break;
        }
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            updateLoginBtnState();
        }
    };

    private void updateLoginBtnState() {
        v.setEnabled(R.id.login_btn, v.getText(R.id.login_username_edit).length() > 0 && v.getText(R.id.login_password_edit).length() > 0);
    }

    //登录帐号获取token
    private void loginHttp() {
        String username = v.getText(R.id.login_username_edit);
        String password = v.getText(R.id.login_password_edit);
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);
        HttpUtils.postString(json).login().enqueue(new HttpNormalCallback<UserHttpBean>(this) {
            @Override
            protected void onSuccess(UserHttpBean data) {
                UserBean bean = data.data;
                AppDataUtil.getInstance().commitData(bean);
                loginImHttp(bean.accountToken);
            }

            @Override
            protected void onFail(int code, String url, String msg) {
                DialogManager.getInstance().showContent(LoginActivity.this
                        , new ContentDialog.Options().setContent(msg));
                closeLoadingView();
            }
        }.setShowLoading(true).setLoadingFinishClose(false));
    }

    //登录im
    private void loginImHttp(String accountToken) {
        SIMManager.getInstance().login(accountToken, new LoginCallbackListener() {
            @Override
            public void onSucceed() {
                closeLoadingView();
                skipActivityFinish(MainActivity.class);
            }

            @Override
            public void onFail() {
                closeLoadingView();
                DialogManager.getInstance().showContent(LoginActivity.this
                        , new ContentDialog.Options().setContent(getString(R.string.string_00010)));
            }
        });
    }
}
